package com.ktgj.customer.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.customer.common.AxInsuranceUtils;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.domain.Insurance.axModel.*;
import com.ktgj.customer.domain.Insurance.axModel.RequestHeader;
import com.ktgj.customer.domain.vo.AssistProductVo;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.AssistProduct;
import com.ktgj.customer.service.IAssistProductService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 辅营产品Controller
 *
 * @author ruoyi
 * @date 2021-12-20
 */
@RestController
@RequestMapping("/assistProduct")
public class AssistProductController extends BaseController {
    @Autowired
    private IAssistProductService assistProductService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询辅营产品列表
     */
    //@PreAuthorize(hasPermi = "customer:product:list")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody AssistProduct assistProduct) {
        List<AssistProduct> list = assistProductService.selectAssistProductList(assistProduct);
        return AjaxResult.success(list);
    }

    /**
     * 导出辅营产品列表
     */
    @PreAuthorize(hasPermi = "customer:product:export")
    @Log(title = "辅营产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssistProduct assistProduct) throws IOException {
        List<AssistProduct> list = assistProductService.selectAssistProductList(assistProduct);
        ExcelUtil<AssistProduct> util = new ExcelUtil<AssistProduct>(AssistProduct.class);
        util.exportExcel(response, list, "辅营产品数据");
    }

    /**
     * 获取辅营产品详细信息
     */
    @PreAuthorize(hasPermi = "customer:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(assistProductService.selectAssistProductById(id));
    }

    /**
     * 新增辅营产品
     */
    @PreAuthorize(hasPermi = "customer:product:add")
    @Log(title = "辅营产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssistProduct assistProduct) {
        return toAjax(assistProductService.insertAssistProduct(assistProduct));
    }

    /**
     * 修改辅营产品
     */
    @PreAuthorize(hasPermi = "customer:product:edit")
    @Log(title = "辅营产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssistProduct assistProduct) {
        return toAjax(assistProductService.updateAssistProduct(assistProduct));
    }

    /**
     * 删除辅营产品
     */
    @PreAuthorize(hasPermi = "customer:product:remove")
    @Log(title = "辅营产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assistProductService.deleteAssistProductByIds(ids));
    }

    @PostMapping("/queryAssistProductList")
    public List<AssistProductVo> queryAssistProductList(@RequestBody Map<String, Object> map) {
        String status = map.get("status").toString();
        String businessType = map.get("businessType").toString();
        if (StringHelper.isNullOrEmpty(status) || StringHelper.isNullOrEmpty(businessType)) {
            return null;
        }
        List<AssistProductVo> assistProductVos = assistProductService.queryAssistProductList(status, businessType);
        return assistProductVos;
    }

    @PostMapping("/assistProduct")
    public AssistProductVo assistProduct(@RequestBody Map<String, Object> map) {
        String status = map.get("status").toString();
        String businessType = map.get("businessType").toString();
        Long id = Long.valueOf(map.get("id").toString());
        if (StringHelper.isNullOrEmpty(status) || StringHelper.isNullOrEmpty(businessType)) {
            return null;
        }
        AssistProductVo assistProductVo = assistProductService.assistProduct(status, businessType, id);
        return assistProductVo;
    }

    /**
     * 远程调用
     *
     * @param id
     * @return
     */
    @PostMapping("/queryAssistProduct")
    public AjaxResult queryAssistProduct(@RequestParam("id") Long id) {
        AssistProduct assistProduct = assistProductService.queryAssistProduct(id);
        return AjaxResult.success(assistProduct);
    }

    /**
     * 新增辅营通用产品
     */
    @PostMapping("/insert")
    public AjaxResult insert(HttpServletRequest request, @RequestBody AssistProduct assistProduct) {
        Employee employee = loginService.getLoginEmployee(request);
        if (employee == null){
            return AjaxResult.error("登录已过期");
        }
        assistProduct.setCremployeeid(employee.getEmployeeId());
        assistProduct.setOpemployeeid(employee.getEmployeeId());
        return toAjax(assistProductService.insert(assistProduct));
    }

    /**
     * 修改辅营通用产品
     */
    @PostMapping("/update")
    public AjaxResult update(HttpServletRequest request, @RequestBody AssistProduct assistProduct) {
        Employee employee = loginService.getLoginEmployee(request);
        assistProduct.setOpemployeeid(employee.getEmployeeId());
        return toAjax(assistProductService.update(assistProduct));
    }

    /**
     * 查询辅营通用产品列表
     */
    @GetMapping("/select")
    public TableDataInfo select(AssistProduct assistProduct) {
        startPage();
        return getDataTable(assistProductService.select(assistProduct));
    }

    /**
     * 删除辅营通用产品
     */
    @PostMapping("/delete")
    public AjaxResult delete(HttpServletRequest request, @RequestBody AssistProduct assistProduct) {
        Employee employee = loginService.getLoginEmployee(request);
        assistProduct.setOpemployeeid(employee.getEmployeeId());
        assistProduct.setDeleted(1l);
        return toAjax(assistProductService.delete(assistProduct));
    }

    /**
     * 通用产品-产品选择下拉列表
     */
    @PostMapping("queryProductSelect")
    public AjaxResult queryProductSelect(@RequestBody Map<String, String> map) {
        return AjaxResult.success(assistProductService.queryProductSelect(map.get("productType")));
    }
}
